package com.copro.chemdb.stat.rule;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ihelpuoo.db.sql.SQLSegment;
import org.ihelpuoo.util.Properties;

import com.copro.chemdb.stat.AbstractStatRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.subject.Subject;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;
import com.copro.simple.db.FreeCallback;
import com.copro.simple.db.SQLSet;

public class Rule5 extends AbstractStatRule {

	private Properties conf;

	static class FreeGetter implements FreeCallback {

		private List<Map<String, Object>> list;
		private Properties conf;
		private int type;

		public FreeGetter(Properties conf, int type) {
			this.list = new ArrayList<Map<String, Object>>();
			this.conf = conf;
			this.type = type;
		}

		public List<Map<String, Object>> getList() {
			return this.list;
		}

		@Override
		public void exec(Connection conn, SQLSet sqls) throws SQLException {
			Application app = (Application) sqls;
			Statement stm = conn.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			Statement stmB = conn.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			SQLSegment sql = null;
			String bussCode = conf.getStringTrimed("bussCode");
			String comCode = conf.getStringTrimed("comCode");
			String dbCode = conf.getStringTrimed("dbCode");
			sql = app.getSQL("rule5CountDatabase");
			sql.set("bussCode", bussCode);
			sql.set("type", type);
			ResultSet rs = stm.executeQuery(sql.toString());
			sql = app.getSQL("rule5ListDatabaseByCompany");
			sql.set("bussCode", bussCode);
			sql.set("comCode", comCode);
			sql.set("dbCode", dbCode);
			sql.set("type", type);
			int bussCount;
			String bussName, comName, dbNames;
			ResultSet rsB;
			while (rs.next()) {
				bussName = rs.getString("name");
				bussCount = rs.getInt("count");
				Map<String, Object> buss = new HashMap<String, Object>();
				Map<String, Object> bussHead = new HashMap<String, Object>();
				bussHead.put("name", bussName);
				bussHead.put("count", bussCount);
				buss.put("header", bussHead);
				sql.set("industry", bussName);
				sql.set("type", type);
				rsB = stmB.executeQuery(sql.toString());
				List<Map<String, Object>> bussBodySet = new ArrayList<Map<String, Object>>();
				Map<String, Object> bussBody;
				while (rsB.next()) {
					comName = rsB.getString("strvalue");
					dbNames = rsB.getString("dbs");
					bussBody = new HashMap<String, Object>();
					bussBody.put("comName", comName);
					bussBody.put("dbNames", dbNames);
					bussBodySet.add(bussBody);
				}
				buss.put("bodys", bussBodySet);
				list.add(buss);
			}
		}
	}

	@Override
	public StatResult doStat(Application app, Subject subject, int type)
			throws SimpleException {
		StatResult statResult = new StatResult();
		FreeGetter fg = new FreeGetter(conf, type);
		app.exec(fg);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("bussSet", fg.getList());
		result.put("text", subject.getBrief());
		statResult.setResult(result);
		statResult.setForwardName("rule5");
		return statResult;
	}

	@Override
	public void setup(Properties conf) {
		this.conf = conf;
	}

}
